import http from '../http'

export const Header = {
  authToken() {
    let token = ''
    const profile = localStorage.getObject('profile')
    if (profile && profile.token) {
      token = profile.token
    }
    return {
      'x-auth-token': token
    }
  }
}

export const Member = {
  isEmailUsed(email) {
    return http.$get('/member/email/used', { 'email': email })
  },
  register(payload) {
    return http.post('/member/register', payload)
  },
  login(payload) {
    return http.post('/member/login', payload, { globalErrorHandle: false })
  },
  logout() {
    return http.post('/member/logout')
  },
  profile() {
    return http.get('/member/profile')
  },
  updateProfile(payload) {
    return http.post('/member/profile/update', payload)
  },
  updatePwd(payload) {
    return http.post('/member/pwd/update', payload)
  },
  enable(memberId) {
    return http.post('/admin/member/enable', {'memberId': memberId})
  },
  disable(memberId) {
    return http.post('/admin/member/disable', {'memberId': memberId})
  }
}

export const Items = {
  listUsjItems(query) {
    return http.$get('/admin/usj/item/list', query)
  },
  listItems(query) {
    return http.$get('/admin/item/list', query)
  },
  syncUSJItemCost(itemId) {
    return http.post('/admin/item/usj/price?itemId=' + itemId)
  },
  syncUSJItemStock(itemId) {
    return http.post('/admin/item/usj/cost?itemId=' + itemId)
  }
}

export const Orders = {
  getOrder(orderNumber) {
    return http.$get('/admin/order/get', { orderNumber: orderNumber })
  },
  listOrders(query) {
    return http.$get('/admin/order/list', query)
  },
  listTravellers(orderNumber) {
    return http.$get('/admin/order/traveller/list', { orderNumber: orderNumber })
  },
  process(orderNumber) {
    return http.post('/admin/order/process', { orderNumber: orderNumber })
  },
  autoDispatch(orderNumber) {
    return http.post('/admin/order/auto-dispatch', { orderNumber: orderNumber })
  },
  autoDispatchItems(orderNumber) {
    return http.post('/admin/order/auto-dispatch-items', { orderNumber: orderNumber })
  },
  processed(orderNumber) {
    return http.post('/admin/order/processed', { orderNumber: orderNumber })
  },
  sendVoucher(orderNumber) {
    return http.post('/admin/order/voucher/send', { orderNumber: orderNumber })
  },
  listPurchaseOrders(orderNumber) {
    return http.$get('/admin/order/purchase-order/list', { orderNumber: orderNumber })
  },
  getMerchantItemCode(orderNumber) {
    return http.$get('/admin/order/merchant/item/code', { orderNumber: orderNumber })
  },
  downloadReceipt(orderNumber) {
    return http.$get('/admin/order/receipt', { orderNumber: orderNumber })
  }
}

export const OrderContact = {
  getTop3() {
    return http.get('/order-contact/top3')
  }
}

export const Balance = {
  get(dtbId) {
    return http.get('/admin/balance/get?dtbId=' + dtbId)
  },
  details(query) {
    return http.$get('/admin/balance/details', query)
  },
  accounts(query) {
    return http.$get('/admin/balance/accounts', query)
  }
}

export const Dtb = {
  get(dtbId) {
    return http.get('/admin/dtb/get?dtbId=' + dtbId)
  },
  list(query) {
    const q = Object.assign({pagesize: 20}, query)
    return http.$get('/admin/dtb/list', q)
  },
  update(payload) {
    return http.post('/admin/dtb/update', payload)
  },
  review(dtbId, approved, remark) {
    return http.post('/admin/dtb/review', { dtbId: dtbId, 'approved': approved, 'remark': remark })
  },
  getSetting(dtbId) {
    return http.$get('/admin/dtb/setting/get', { dtbId: dtbId })
  },
  updateSetting(payload) {
    return http.post('/admin/dtb/setting/update', payload)
  },
  listStat(query) {
    return http.$get('/admin/dtb/stat/list', query)
  }
}

export const DtbRecharge = {
  list(query) {
    return http.$get('/admin/dtb/recharge/list', query)
  },
  approve(rechargeId) {
    return http.post('/admin/dtb/recharge/approve?rechargeId=' + rechargeId)
  },
  reject(rechargeId) {
    return http.post('/admin/dtb/recharge/reject?rechargeId=' + rechargeId)
  }
}

export const ExchangeRate = {
  list(query) {
    return http.$get('/admin/exchange-rate/list', query)
  },
  history(query) {
    return http.$get('/admin/exchange-rate/history', query)
  }
}

export const CreditCard = {
  list(query) {
    return http.$get('/admin/credit-card/list', query)
  },
  get(creditCardId) {
    return http.$get('/admin/credit-card/get', { 'creditCardId': creditCardId })
  },
  update(payload) {
    return http.post('/admin/credit-card/update', payload)
  },
  add(payload) {
    return http.post('/admin/credit-card/add', payload)
  }
}

export const Refund = {
  get(refundNumber) {
    return http.$get('/admin/refund/get', {'refundNumber': refundNumber})
  },
  list(query) {
    return http.$get('/admin/refund/list', query)
  },
  display(refundNumber) {
    return http.$get('/admin/refund/display', {'refundNumber': refundNumber})
  },
  apply(payload) {
    return http.post('/admin/refund/apply', payload)
  },
  reject(refundNumber) {
    return http.post('/admin/refund/reject', {'refundNumber': refundNumber})
  },
  approve(refundNumber) {
    return http.post('/admin/refund/approve', {'refundNumber': refundNumber})
  },
  refund(refundNumber) {
    return http.post('/admin/refund/refund', {'refundNumber': refundNumber})
  }
}

export const PurchaseOrder = {
  display(purchaseOrderId) {
    return http.$get('/admin/purchase/order/display', {'purchaseOrderId': purchaseOrderId})
  },
  list(query) {
    return http.$get('/admin/purchase/order/list', query)
  },
  downloadVoucher(purchaseOrderId) {
    return http.$get('/admin/purchase/order/voucher/download', {'purchaseOrderId': purchaseOrderId})
  }
}

export const Utils = {
  sum(data, column) {
    return data.reduce((prev, curr) => {
      const value = Number(curr[column])
      return isNaN(value) ? prev : prev + value
    }, 0)
  }
}
